package com.micro.modual.service.impl;

import com.micro.common.MicroResponseCode;
import com.micro.core.exception.BizException;
import com.micro.core.frame.service.impl.AbstractBasicsServiceImpl;
import com.micro.core.resp.RespJson;
import com.micro.core.resp.RespJsonData;
import com.micro.modual.entity.SystemMenu;
import com.micro.modual.extension.SystemMenuExtension;
import com.micro.modual.mapper.SystemElementMapper;
import com.micro.modual.mapper.SystemMenuMapper;
import com.micro.modual.mapper.SystemResourceAuthMapper;
import com.micro.modual.model.bo.systemmenu.MenuStatusBO;
import com.micro.modual.model.bo.systemmenu.SystemMenuBO;
import com.micro.modual.model.tree.MenuTreeDTO;
import com.micro.modual.model.tree.SystemMenuTreeDTO;
import com.micro.modual.service.SystemMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
*  <h1>系统菜单表  实现</h1>
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020-08-04 00:11:10
 */
@Service("systemMenuService")
public class SystemMenuServiceImpl extends AbstractBasicsServiceImpl<SystemMenu,Long> implements SystemMenuService {

    @Autowired
    private SystemMenuMapper systemMenuMapper;

    @Autowired
    private SystemResourceAuthMapper systemResourceAuthMapper;

    @Autowired
    private SystemElementMapper systemElementMapper;

    @Override
    public RespJsonData<List<SystemMenuTreeDTO>> findSystemMenuList(Map<String, Object> params) {
        List<SystemMenu> systemMenuList = systemMenuMapper.findSystemMenuList(params);
        List<SystemMenuTreeDTO> systemMenuListDTOList = SystemMenuExtension.convertSystemMenuListDTO(systemMenuList);
        return new RespJsonData<List<SystemMenuTreeDTO>>(systemMenuListDTOList, MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

    /**
     * <h2>新增系统菜单表详情</h2>
     * @param systemMenuBO {@link SystemMenuBO} 系统菜单表 新增/修改 请求对象
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson addSystemMenu(SystemMenuBO systemMenuBO) {
        SystemMenu systemMenu = SystemMenuExtension.converAddSystemMenu(systemMenuBO);
        systemMenuMapper.insertSelective(systemMenu);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(), MicroResponseCode.SUCCESS.getResponseMessage());
    }

    /**
     * <h2>修改系统菜单表详情</h2>
     * @param systemMenuBO  {@link SystemMenuBO} 系统菜单表 新增/修改 请求对象
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson editSystemMenu(SystemMenuBO systemMenuBO) {
        SystemMenu systemMenu = SystemMenuExtension.converEditSystemMenu(systemMenuBO);
        systemMenuMapper.updateByPrimaryKeySelective(systemMenu);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

    /**
     * <h2>根据主键编号删除系统菜单表信息</h2>
     * @param id  系统菜单表 主键编号
     * @return  {@link RespJson} 返回结果基础类
     */

    @Override
    public RespJson deleteSystemMenu(Long id) {
        SystemMenu systemMenu = systemMenuMapper.selectByPrimaryKey(id);
        if(systemResourceAuthMapper.hasResourceDistribution(systemMenu.getMenuCode()) > 0 ){
            throw new BizException(MicroResponseCode.MENU_HAS_DISTRIBUTION_NOT_ALLOW_DELETE.getResponseCode(),MicroResponseCode.MENU_HAS_DISTRIBUTION_NOT_ALLOW_DELETE.getResponseMessage());
        }
        if(systemMenuMapper.hasChild(systemMenu.getMenuCode()) > 0 ){
            throw new BizException(MicroResponseCode.MENU_HAS_CHILD.getResponseCode(),MicroResponseCode.MENU_HAS_CHILD.getResponseMessage());
        }

        systemElementMapper.deleteByMenuCode(systemMenu.getMenuCode());
        systemMenuMapper.deleteByPrimaryKey(id);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }

    /**
     * <h2>修改菜单状态</h2>
     * @param menuStatusBO {@link MenuStatusBO} 菜单状态请求对象
     * @return
     */
    @Override
    public RespJson modifyMenuStatus(MenuStatusBO menuStatusBO) {
        SystemMenu systemMenu = systemMenuMapper.selectByPrimaryKey(menuStatusBO.getMenuId());
        systemMenu.setMenuStatus(menuStatusBO.getMenuStatus());
        systemMenuMapper.updateByPrimaryKeySelective(systemMenu);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

    /**
     * <h2>获取所有菜单树信息</h2>
     * @return
     */
    @Override
    public RespJsonData<List<MenuTreeDTO>> findMenuTree(Map<String, Object> map) {
        List<SystemMenu> menuList = systemMenuMapper.findSystemMenuList(map);
        List<MenuTreeDTO> menuTreeList = SystemMenuExtension.buildMenuTree(menuList);
        return new RespJsonData<List<MenuTreeDTO>>(menuTreeList,MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

    /**
     * <h2>查询所有菜单信息</h2>
     * @return {@link SystemMenu} s 菜单
     */
    @Override
    public RespJsonData<List<SystemMenu>> findAllMenu() {
        List<SystemMenu> menuList = systemMenuMapper.selectAll();
        return new RespJsonData<List<SystemMenu>>(menuList, MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }

    /**
     * <h2>校验菜单是否存在</h2>
     * @param menuName 菜单名称
     * @return
     */
    @Override
    public RespJson validataMenuExist(String menuName) {
        if(systemMenuMapper.findMenuExist(menuName) > 0 ){
            throw new BizException(MicroResponseCode.MENU_HAS_EXIST.getResponseCode(),MicroResponseCode.MENU_HAS_EXIST.getResponseMessage());
        }else{
            return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
        }
    }

}
